package fishBids.group3;



import jade.content.lang.Codec;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.SequentialBehaviour;
import jade.util.leap.ArrayList;
import jade.util.leap.Collection;
import jade.util.leap.Iterator;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import es.upm.agents.ontology.Fish_BidsOntology;
import es.upm.agents.ontology.ListProduct;
import es.upm.agents.ontology.Product;
import fishBids.group3.behaviour.auctioneer.AuctioneerAuctionOverBehaviour;
import fishBids.group3.behaviour.auctioneer.AuctioneerPriceVariatedBehaviour;
import fishBids.group3.behaviour.auctioneer.AuctioneerRegisterBehaviour;
import fishBids.group3.behaviour.common.FindTypeBehaviour;
import fishBids.group3.common.Constants;
import fishBids.group3.utils.Utils;

/**

 * This agent has the following functionality: 

 * <ul>

 * <li> It registers itself in the DF as Auctioneer

 * <li> Waits for requests of its registering service

 * <li> If any registering request arrives, it answers

 * </ul>

 * @author Gabriel & Mai, UPM

 * @version $Date: 2009/04/30 13:45:18 $ $Revision: 1.1 $

 **/





public class AgAuctioneer extends Agent {

	private static final long serialVersionUID = 7237574461358020802L;

	private Codec codec = new SLCodec(); 
	private Ontology ontology = Fish_BidsOntology.getInstance(); 
	private final AID bankID = new AID();
	private final AID marketID = new AID();
	private boolean itemOver = true;
	private boolean auctionStart = false;
	private boolean auctionOver = false;
	private boolean registrationOver = false;
	private Collection biddersRegistered = new ArrayList();
	private Iterator productsIt;
	private ListProduct listProduct = readListOfProducts();
	private Product product;

	
	protected void setup()

	{

		System.out.println("Auctioneer3: enters into the system");
		this.getContentManager().registerLanguage(codec); 
		this.getContentManager().registerOntology(ontology);
		
		Utils.registerInDF(this.getName(), Constants.AUCTIONEER, this);
		doWait(10000);

//		BEHAVIOURS ****************************************************************



		addBehaviour(new FindTypeBehaviour( Constants.BANK,this,bankID,5000));
		addBehaviour(new FindTypeBehaviour( Constants.MARKET,this,marketID,5000));
	    addBehaviour(new AuctioneerRegisterBehaviour(this,codec,ontology, listProduct) );
	    SequentialBehaviour BidProcess = new SequentialBehaviour(this);
	    productsIt = listProduct.getAllProducts();
	    while( (productsIt.hasNext()) || (!this.isItemOver())){
	    	product = (Product) productsIt.next();
	    	BidProcess.addSubBehaviour(new AuctioneerPriceVariatedBehaviour(this, product, marketID) );
	    }
	    
	    BidProcess.addSubBehaviour(new AuctioneerAuctionOverBehaviour(this));
	    addBehaviour(BidProcess);
	    }

   

	public AID getBankID() {
		return bankID;
	}
	
	public AID getMarketID() {
		return marketID;
	}

	public boolean isItemOver() {
		return itemOver;
	}

	public void setItemOver(boolean itemOver) {
		this.itemOver = itemOver;
	}

	public Collection getBiddersRegistered() {
		return biddersRegistered;
	}

	public void setBiddersRegistered(Collection biddersRegistered) {
		this.biddersRegistered = biddersRegistered;
	}
	
	public void addBidderToBiddersRegistered(AID aid){
		
		this.getBiddersRegistered().add(aid);
		
	}

	public Codec getCodec() {
		return codec;
	}

	public void setCodec(Codec codec) {
		this.codec = codec;
	}

	public Ontology getOntology() {
		return ontology;
	}

	public void setOntology(Ontology ontology) {
		this.ontology = ontology;
	}

	public boolean isRegistrationOver() {
		return registrationOver;
	}

	public void setRegistrationOver(boolean registrationOver) {
		this.registrationOver = registrationOver;
	}

	public boolean isAuctionStart() {
		return auctionStart;
	}

	public void setAuctionStart(boolean auctionStart) {
		this.auctionStart = auctionStart;
	}

	public ListProduct readListOfProducts() {

		File fFile = new File("goods.txt");
		Scanner scanner = null;
		Scanner scanner2 = null;
		ListProduct listProduct = new ListProduct();
		
		try {
			scanner = new Scanner(fFile);
			while (scanner.hasNextLine()) {
				scanner2 = new Scanner(scanner.nextLine());
				scanner2.useDelimiter(" ");
				if (scanner2.hasNext()) {
					String type = scanner2.next();
					String amount = scanner2.next();
					//System.out.println("type: " + type + "\tAmount: "
					//		+ amount + "\n");
					
					Product product = new Product();
					product.setProductType( 
							type.equalsIgnoreCase(Constants.tuna)     ? Constants.TUNA_TYPE :
							type.equalsIgnoreCase(Constants.salmon)   ? Constants.SALMON_TYPE :
							type.equalsIgnoreCase(Constants.calamari) ? Constants.CALAMARI_TYPE :
							type.equalsIgnoreCase(Constants.prawn)    ? Constants.PRAWN_TYPE : null );
					product.setAmount(Float.parseFloat(amount));
					listProduct.addProducts(product);
				}
				scanner2.close();

			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			scanner.close();
		}

		return listProduct;
	}



	
	
	
}

